<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-dialog title="新增权限" :visible.sync="dialogFormVisible">
                <el-form :model="addForm" :rules="rules" size="small" ref="addForm">
                    <el-form-item label="权限" :label-width="formLabelWidth" prop="name">
                        <el-input v-model="addForm.name" placeholder="请输入内容"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
            <el-dialog title="修改权限" :visible.sync="dialogFormUpdate">
                <el-form :model="upForm" :rules="rules" size="small" ref="upForm">
                    <el-form-item label="权限" :label-width="formLabelWidth" prop="name">
                        <el-input v-model="upForm.name" placeholder="请输入内容"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormUpdate = false">取 消</el-button>
                    <el-button type="primary" @click="modifyRole">确 定</el-button>
                </div>
            </el-dialog>
            <el-input v-model="select_word" placeholder="输入权限名称" class="search"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
            <el-button type="primary" @click="dialogFormVisible = true">新增权限</el-button>
        </div>
        <div>
            <el-table :data="tableData" border fit style="width: 100%">
                <el-table-column prop="id" label="权限id">
                </el-table-column>
                <el-table-column prop="p_name" label="权限">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button type="text" size="small" @click="delRole(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :current-page="page.number" 
            layout="total, prev, pager, next, jumper" :total="page.total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { user } from "../../server/getDate.js";
import { mapState, mapActions } from 'vuex'
import { getStore } from '../../utils/local.js'
import { confirm } from '../../utils/local'
const { privilegeList, addPrivilege, deletePrivilege, updatePrivilege } = user 

export default {
    data() {
        return {
            value: '',
            loading: false,
            dialogTableVisible: false,
            dialogFormVisible: false,
            dialogFormUpdate: false,
            select_word: '',
            upForm: {
                name: '',
                id: ''
            },
            addForm: {
                name: '',
            },
            formLabelWidth: '80px',
            rules: {
                name: [{ required: true, message: '请输入权限', trigger: 'blur' }]
            }
        }
    },
    created() {
        this.getPrivilege(1)
    },
    computed: mapState({
        page: state => state.Privilege.page,
        tableData: state => state.Privilege.list
    }),
    methods: {
        ...mapActions(['savePrivilege']),
        getPrivilege(number) {
            const obj = {
                page: number,
                pageSize: 10
            }
            if (this.select_word) {
                obj.searchKey = this.select_word
            }
            privilegeList({ ...obj}).then(res => {
                    if (res.code === '000000') {
                        this.savePrivilege({
                            ...res,
                            number
                        })
                    }
                })
        },
        handleClick(row) {
            console.log(row)
        },
        search() {
            this.getPrivilege(this.page.number)
        },
        handleCurrentChange(val) {
            this.getPrivilege(val)
        },
        delRole(row) {
            this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let id = row.id
                deletePrivilege({ id })
                    .then(res => {
                        if (res.code === '000000') {
                            this.getPrivilege(this.page.number)
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        add() {
            this.$refs['addForm'].validate((valid) => {
                if (valid) {
                    const role = {
                        privilegeName: this.addForm.name,
                    }
                    addPrivilege({ ...role })
                        .then(res => {
                            if (res.code === '000000') {
                                this.dialogFormVisible = false
                                this.$message({
                                    type: 'success',
                                    message: '添加成功!'
                                });
                                this.addForm = {}
                                if (this.page.number >= this.page.total / this.tableData.length) {
                                    this.getPrivilege(this.page.number)
                                } else {
                                    this.getPrivilege(this.page.number + 1)
                                }
                            } else {
                                this.$message.error(res.message)
                            }
                        }, err => this.$message.error("网络异常"))                    
                }
            })
        },
        edit(row) {
            this.dialogFormUpdate = true
            this.upForm.name = row.p_name
            this.upForm.id = row.id
        },
        modifyRole() {
            this.$refs['upForm'].validate((valid) => {
                if (valid) {
                    let roleEntity = this.upForm
                    updatePrivilege({ 
                        id: roleEntity.id,
                        privilegeName: roleEntity.name
                    })
                        .then(res => {
                            if (res.code === '000000') {
                                this.dialogFormUpdate = false
                                this.getPrivilege(this.page.number)
                            } else {
                                this.$message.error(res.message)
                            }
                        }, err => this.$message.error(res.message)) 
                }
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>